package com.hnl.action.user;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.dao.RoleDAO;
import com.hnl.dao.UserDAO;
import com.hnl.to.Role;
import com.hnl.to.User;

@SuppressWarnings("serial")
public class AddConsultant extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory
			.getLogger(AddConsultant.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		try {
			Integer userID = Integer.valueOf(req.getParameter("userID"));
			Integer roleNameID = Integer.valueOf(req.getParameter("roleDropdown"));
			HttpSession session = ((HttpServletRequest) req).getSession();
			User userSession = (User) session.getAttribute(ATTRIBUTE_USER);

			UserDAO userDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getUserDAO();
			User newUser;
			Date now = new Date();
			
			RoleDAO roleDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getRoleDAO();
			Role role = roleDAO.loadRoleByRoleName(roleNameID);

			if (userID.equals(0)) {
				newUser = new User();
			} else {
				newUser = userDAO.loadUserByUserID(Integer.valueOf(userID));
			}

			newUser.setFirstname(req.getParameter("firstName"));
			newUser.setLastname(req.getParameter("lastName"));
			newUser.setEmail(req.getParameter("email"));
			newUser.setRoleID(role.getRoleID());
			
			if (userID.equals(0)) {
				newUser.setPassword(req.getParameter("newPassword"));
				newUser.setLastLoginDate(null);
				newUser.setCreateDate(now);
				newUser.setCreateUserID(userSession.getUserID());
			} else {
				newUser.setPassword(newUser.getPassword());
				newUser.setLastLoginDate(newUser.getLastLoginDate());
				newUser.setCreateDate(newUser.getCreateDate());
				newUser.setCreateUserID(newUser.getCreateUserID());
			}
			newUser.setIsDelete(false);
			newUser.setModifyDate(now);
			newUser.setModifyUserID(userSession.getUserID());

			if (userID.equals(0)) {
				newUser = userDAO.saveUser(newUser);
				userID = newUser.getUserID();
			} else {
				userDAO.updateUser(newUser);
			}

			req.setAttribute("userID", userID);

			RequestDispatcher dispatcher = req
					.getRequestDispatcher(USER_INFO_PAGE);
			dispatcher.forward(req, resp);

		} catch (DAOException e) {
			LOG.error("Error to add activity!!", e);
		}

	}

}
